<?php

!defined('IN_SYSTEM') && exit('Access denied');

class portalControlPage extends commonControlPage {

    public function mainModule() {
        $this->view->render('admincp/portal');
    }

	/*修改密码*/
    public function modi_passwordModule() 
	{
        if (PSOT && AJAX) {
            
            if (!$this->verifyCheck($_POST['verify'])) {
                $this->outputJson(-1,'验证码错误');
            }

            if ('' == trim($_POST['new_password'])) {
                $this->outputJson(-2,'新密码不能为空');
            }

            $password = md5(trim($_POST['password']));
            $new_password = md5(trim($_POST['new_password']));
            $new_password_confirm = md5(trim($_POST['new_password_confirm']));

            if ($new_password != $new_password_confirm) {
                $this->outputJson(-3,'两次密码输入不一致');
            }

            $member = $_ENV['db']->fetch("SELECT `password` FROM `admincp_member` WHERE `uid` = {$this->user['uid']}");
            if ($member['password'] != $password) {
                $this->outputJson(-4,'当前密码错误');
            }

            $_ENV['db']->runSql("UPDATE `admincp_member` SET `password` = '{$new_password}' WHERE `uid` = '{$this->user['uid']}'");
            $this->outputJson(1,'密码修改成功，下次请以新密码登陆');
        }

        $this->view->menu_name = "{$this->user['username']}({$this->user['nickname']})";
        $this->view->menu_group_name = '个人中心';
        $this->view->node_name = '修改密码';
        $this->showMainPage('admincp/modi_password');
    }

    /**
     * 获取指定类型的分类
     */
    public function getclassModule() {
        $type = $_REQUEST['type'];
        $object_id = (int)$_REQUEST['object_id'];

        $response = array();

        if (!in_array($type,array('region','product','article'))) {
            echo json_encode($response);
            exit;
        }

        $table = strtr($type,array('region'=>'region','product'=>'product_class','article'=>'article_class'));

        $response = $_ENV['db']->fetchAll("SELECT * FROM `{$table}` WHERE `parent_id` = '{$object_id}'");

        echo json_encode($response);
    }

    /**
     * 检查新订单
     */
    public function cnoModule() {

        // 获取当前用户拥有操作的权限节点ID
        $nodes = array();
        $tmp = $_ENV['db']->fetchAll("SELECT * FROM admincp_member_perm where uid = '{$this->user['uid']}'");
        foreach((array)$tmp as $node) {
            $nodes[] = $node['node_id'];
        }

        
		$html = '<ul style="list-style-type:square">';
        $data_html = '';

        // 订单分配
        if (in_array(29,$nodes)) {
            $count = $_ENV['db']->fetchCol("SELECT count(1) FROM `order` WHERE audit_state = '0'");
            if ($count) {
                $data_html .= '<li><a href="?app=order&mod=audit0">有'.$count.'笔订单等待您分配</a></li>';
            }
        }
        // 订单操作
        // 订单操作
        $oa = array();        
        $orders = $_ENV['db']->fetchAll("SELECT * FROM `order` where co_member_id = '{$this->user['uid']}'");
        foreach ((array)$orders as $order) {
            $tmp = str_replace(array(1,2,3,4,5,6),array('queren','chushen','fushen','chuli','chuku','caigou'),$order['audit_state']);
            $oa[$tmp]+=1;
        }
        if ($oa['queren']) {
            $data_html .= '<li><a href="?app=order&mod=audit1">有'.$oa['queren'].'笔订单等待您确认</a></li>';
        }
        if ($oa['chushen']) {
            $data_html .= '<li><a href="?app=order&mod=audit2">有'.$oa['chushen'].'笔订单等待您初审</a></li>';
        }
        if ($oa['fushen']) {
            $data_html .= '<li><a href="?app=order&mod=audit3">有'.$oa['fushen'].'笔订单等待您复审</a></li>';
        }
        if ($oa['chuli']) {
            $data_html .= '<li><a href="?app=order&mod=audit4">有'.$oa['chuli'].'笔订单等待您处理</a></li>';
        }
        if ($oa['chuku']) {
            $data_html .= '<li><a href="?app=order&mod=audit5">有'.$oa['chuku'].'笔订单等待您出库</a></li>';
        }
        if ($oa['caigou']) {
            $data_html .= '<li><a href="?app=order&mod=audit6">有'.$oa['caigou'].'笔订单等待您采购</a></li>';
        }
		
		/*[借款总额提示]*/
		$userdetail = $_ENV['db']->fetch("SELECT * FROM `admincp_member_detail` WHERE `uid` = '{$this->user['uid']}}'");
		$data_html .= '<li><a href="?app=loan&mod=my">有'.$userdetail['loan_price'].'元欠款未还</a></li>';
		$data_html .= '<li><a href="?app=vacation&mod=my">总请假天数为'.$userdetail['total_vacation'].'天</a></li>';
		$sms =  $_ENV['db']->fetchCol("SELECT count(1) FROM `sms` WHERE tid = '{$this->user['uid']}'");
		if($sms )
		{
			$data_html .= '<li><a href="?app=message&mod=message">您有'.$sms.'条短消息</a></li>';
		}
		
		$vacation =  $_ENV['db']->fetchCol("SELECT count(1) FROM `vacation` WHERE uid = '{$this->user['uid']}' and audit_state!='3' ");
		if($vacation )
		{
			$data_html .= '<li><a href="?app=vacation&mod=my">您有'.$vacation.'张请假条未审核</a></li>';
		}
		
		$loan =  $_ENV['db']->fetchCol("SELECT count(1) FROM `loan` WHERE uid = '{$this->user['uid']}' and audit_state!='2' ");
		if($loan)
		{
			$data_html .= '<li><a href="?app=loan&mod=my">您有'.$vacation.'张欠款单未审核</a></li>';
		}
		
		$daily =  $_ENV['db']->fetchCol("SELECT count(1) FROM `daily` WHERE uid = '{$this->user['uid']}' and audit_state!='4' ");
		if($daily)
		{
			$data_html .= '<li><a href="?app=daily&mod=my">您有'.$vacation.'日报未审核</a></li>';
		}
		
		
		$ysum = '';
		$ssum = '';
		$wsum = '';
		/*[检索当前用户所属的订单，状态为待审核、待出库、正在采购、已出库状态，且没有全额付款的订单]*/
		$thisorders = $_ENV['db']->fetchAll(" select * from `order` where audit_state in ('2','3','4','5') and payed < pprice ");
		foreach((array)$thisorders as $key=>$val)
		{
			$tempInfo = @json_decode($val['pc'],1);
			
			/*[已还金额]*/
			$payed = $val['payed'];
			
			$time1  = strtotime($tempInfo[1]['time']);
        	$time2  = strtotime($tempInfo[2]['time']);
			$time3  = strtotime($tempInfo[3]['time']);
			$now = date('Ymd',$_ENV['time']);
		
			$price1  = strtotime($tempInfo[1]['price']);
        	$price2  = strtotime($tempInfo[2]['price']);
			$price3  = strtotime($tempInfo[3]['price']);
		
			if($time1>$now && $price1 > $payed)
			{//预付款时间到了，没回款
				$ysum.=$val['order_sn'].',';
			}
			if($time2>$now && $price2 > $payed)
			{//收货付款时间到了，没回款
				$ssum.=$val['order_sn'].',';
			}
			if($time3>$now && $price3 > $payed)
			{//尾付款时间到了，没回款
				$wsum.=$val['order_sn'].',';
			}
		}
		
		if($ysum)
		{
			$html .='<li><a href="?app=order&mod=list">订单号'.$ysum.'订单预付款到期了</a></li>';
		}
		if($ssum)
		{
			$html .='<li><a href="?app=order&mod=list">订单号'.$ssum.'订单收货付款到期了</a></li>';
		}
		if($wsum)
		{
			$html .='<li><a href="?app=order&mod=list">订单号'.$wsum.'订单尾付款到期了</a></li>';
		}
		
        $html .= $data_html.'</ul>';
        if ($data_html) {
            $this->outputJson(1,$html);
		} else {
			$this->outputJson(0,'');
		}
    }

    /**
     * 服务器已上传文件管理
     */
    private $order = 'NAME';
    public function filemanagerModule() {
        $filenames = $file_list = array();
        $dir = $_GET['dir'];
        $case = $_GET['case'];
        if ($case == 'article') {
            $baseurl = './attach/article/';
            $basepath = realpath(ATTACH_PATH.'article/');
        } else {
            $baseurl = './attach/desc/';
            $basepath = realpath(ATTACH_PATH.'desc/');
        }
        if (empty($_GET['path'])) {
            $current_path = $basepath.'/';
            $current_url = $baseurl;
            $current_dir_path = '';
            $moveup_dir_path = '';
        } else {
            $current_path = realpath($basepath.'/'.$_GET['path']).'/';
            $current_url = $baseurl.$_GET['path'];
            $current_dir_path = $_GET['path'];
            $moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/','$1',$current_dir_path);
        }
        $this->order = in_array($_GET['order'],array('NAME','SIZE','TYPE')) ? $_GET['order'] : 'name';
        if ($dir == 'image' && !preg_match('/\.\./', $current_path) && preg_match('/\/$/', $current_path) && (file_exists($current_path) || is_dir($current_path))) {
            $filenames = scandir($current_path);
            foreach ($filenames as $filename) {
                if (in_array($filename,array('.','..'))) continue;
                $file = $current_path.$filename;
                $files = array();
                if (is_dir($file)) {
                    $files['is_dir'] = true;
                    $files['has_file'] = count(scandir($file)) > 2;
                    $files['filesize'] = 0;
                    $files['is_photo'] = false;
                    $files['filetype'] = '';
                } else {
                    $files['is_dir'] = false;
                    $files['has_file'] = false;
                    $files['filesize'] = filesize($file);
                    $files['dir_path'] = '';
                    $files['is_photo'] = true;
                    $files['filetype'] = end(explode('.',$filename));
                }
                $files['filename'] = $filename;
                $files['datetime'] = date('Y-m-d H:i:s',filemtime($file));
                $file_list[] = $files;
            }
            usort($file_list,array($this,'csort'));
        }
        $output = array();
        $output['moveup_dir_path'] = $moveup_dir_path;
        $output['current_dir_path'] = $current_dir_path;
        $output['current_url'] = $current_url;
        $output['total_count'] = count($file_list);
        $output['file_list'] = $file_list;
        echo json_encode($output);
    }
    private function csort($a,$b){
        if ($a['is_dir'] && !$b['is_dir']) {
            return -1;
        } else if (!$a['is_dir'] && $b['is_dir']) {
            return 1;
        } else {
            if ($this->order == 'SIZE') {
                if ($a['filesize'] > $b['filesize']) {
                    return 1;
                } else if ($a['filesize'] < $b['filesize']) {
                    return -1;
                } else {
                    return 0;
                }
            } else if ($this->order == 'TYPE') {
                return strcmp($a['filetype'], $b['filetype']);
            } else {
                return strcmp($a['filename'], $b['filename']);
            }
        }
    }
}